# 获取jenkins构建信息和本次报告地址
import os
import jenkins #安装pip install python-jenkins
import json
import urllib3


# jenkins登录地址
jenkins_url = "http://127.0.0.1:8282/"
# 获取jenkins对象
server = jenkins.Jenkins(jenkins_url, username='xianzhe', password='xianzhe') #Jenkins登录名 ，密码
# job名称
job_name = "job/gitlab_demo/" #Jenkins运行任务名称
# job的url地址
job_url = jenkins_url + job_name
# 获取最后一次构建
job_last_build_url = server.get_info(job_name)['lastBuild']['url']
# 报告地址
report_url = job_last_build_url + 'allure' #'allure'为我的Jenkins全局工具配置中allure别名

'''
钉钉推送方法：
读取report文件中"prometheusData.txt"，循环遍历获取需要的值。
使用钉钉机器人的接口，拼接后推送text
'''

def DingTalkSend():
    d = {}
    # 获取项目绝对路径
    path = os.path.abspath(os.path.dirname((__file__)))
    # 打开prometheusData 获取需要发送的信息

    print(path + r'\allure-reports\html\export\prometheusData.txt')

    try:
        with open(path + r'/demo.txt', 'r') as f:
            for lines in f:  # 通常这里应该是 'line' 而不是 'lines'，因为每次迭代都是一行
                parts = lines.strip('\n').split(' ')  # 去除换行符并按空格分割
                if len(parts) >= 2:  # 确保至少有两个元素
                    launch_name = parts[0]
                    num = parts[1]
                    d.update({launch_name: num})  # 更新或添加键值对到字典
        print(d)  # 在文件读取完成后打印字典
    except FileNotFoundError:
        print("文件未找到或无法打开")
    except IOError as e:
        print(f"打开文件时发生I/O错误: {e}")
    print(d)
    retries_run = d.get('launch_retries_run')  # 运行总数
    print('运行总数:{}'.format(retries_run))
    status_passed = d.get('launch_status_passed')  # 通过数量
    print('通过数量：{}'.format(status_passed))
    status_failed = d.get('launch_status_failed')  # 不通过数量
    print('没通过数量：{}'.format(status_failed))

    # 钉钉推送

    url = 'https://oapi.dingtalk.com/robot/send?access_token=https://oapi.dingtalk.com/robot/send?access_token=77cb138bd0952cb521ae6aa232304a62e7afb149632c0d6358af02c0f976b7ef'  # webhook
    con = {"msgtype": "text",
           "text": {
               "content": "Pytest_Allure_Demo自动化脚本执行完成。"
                          "\n测试概述:"
                          "\n运行总数:" + retries_run +
                          "\n通过数量:" + status_passed +
                          "\n失败数量:" + status_failed +
                          "\n构建地址：\n" + job_url +
                          "\n报告地址：\n" + report_url
           }
           }
    urllib3.disable_warnings()
    http = urllib3.PoolManager()
    jd = json.dumps(con)
    jd = bytes(jd, 'utf-8')
    http.request('POST', url, body=jd, headers={'Content-Type': 'application/json'})


if __name__ == '__main__':
    DingTalkSend()